package com.stedu.mall.adminservice.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import com.stedu.mall.common.utils.JwtUtil;
import com.stedu.mall.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*

*/

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AdminService adminService;
    @GetMapping("/captcha")
    public RespBean captcha()
    {
        //创建验证码的对象
        LineCaptcha captcha = new LineCaptcha(120, 38,4, 10);
        //生成唯一的key
        String key = IdUtil.getSnowflakeNextIdStr();
        //将key和验证码文本保存在Redis中-超时时间60秒
        redisUtil.set(key, captcha.getCode(),60);
        //将验证码转换成Base64编码的形式
        String imageBase64Data = captcha.getImageBase64Data();
        //封装到map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("imageBase64Data", imageBase64Data);
        //将key和Base64图片返回
        return RespBean.ok("验证码获取成功", map);
    }
    @PostMapping("/login")
    public RespBean login(String username, String password, String key, String captchaInput) throws SteduException {
        //判断验证码是否正确 -从Redis中获取验证码进行判断
        String captcha = (String) redisUtil.get(key);
        if(captcha.equalsIgnoreCase(captchaInput)){
            throw new SteduException("验证码错误");
        }
        //登录
        Admin admin = adminService.login(username, password);
        //生成token
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", admin.getId());
        map.put("username", admin.getUsername());
        map.put("role", "admin");
        String jwt = JwtUtil.generateJwt(map);
        return RespBean.ok("登录成功", jwt);
    }
    //获取登录信息
    @GetMapping("/info")
    public RespBean getLogin(@RequestHeader("token") String token){
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id
        Integer id = (Integer) map.get("id");
        Admin admin = adminService.selectById(id);
        //返回登录信息
        return RespBean.ok("登录信息获取成功", admin);
    }
    @PutMapping("/chgPwd")
    public RespBean chgPwd(String oldPwd, String newPwd,String newPwd1,@RequestHeader("token") String token) throws SteduException {
        if (!newPwd.equals(newPwd1)){
            throw new SteduException("两次输入的新密码不一致");

        }
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id
        Integer id = (Integer) map.get("id");
        adminService.chgPwd(oldPwd, newPwd, id);
        return RespBean.ok("修改密码成功，请重新登录");
    }
    @GetMapping
    public RespBean selectAll()  {
        List<Admin> adminList = adminService.selectAll();
        return RespBean.ok("", adminList);
    }
    //添加
    @PostMapping
    public RespBean insert(@RequestBody Admin admin) throws SteduException {
        admin.setPassword("123456");
        adminService.insert(admin);
        return RespBean.ok("添加成功");
    }
    //修改个人
    @PutMapping("/info")
    public RespBean updateInfo(@RequestHeader("token") String token, @RequestBody Admin admin) {
        //解析token
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        //获取用户id - 根据id、查询
        Integer id = (Integer)map.get("id");
        admin.setId(id);
        admin.setPassword(null);
        //修改信息
        adminService.updateInfo(admin);
        return RespBean.ok("信息修改成功");
    }
    //根据id修改
    @PutMapping("/updateAdmin")
    public RespBean update(@RequestBody Admin admin) {
        admin.setPassword(null);
        adminService.update(admin);
        return RespBean.ok("修改成功");
    }
    @GetMapping("/selectById/{id}")
    public RespBean selectByid(@PathVariable("id") Integer id) {
        Admin a = adminService.selectById(id);
        return RespBean.ok("", a);
    }
    //根据id删除
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws SteduException {
        adminService.delete(id);
        return RespBean.ok("删除成功");
    }
    //重置密码
    @PutMapping("/resetPassword/{id}")
    public RespBean resetPassword(@PathVariable("id") Integer id) throws SteduException {
        adminService.resetPassword(id, "123456");
        return RespBean.ok("密码重置成功");
    }


}


